package com.xiyi_design.printer_manage.pub;

import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

@Component
public class AccessInterceptor  implements HandlerInterceptor {
    private final Logger mLogger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StringRedisTemplate mRedisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        // 设置返回为json格式，使用UTF-8
        /*response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");

        String reqUrl = request.getRequestURL().toString();
        int methodPos = reqUrl.indexOf("/user_manage");
        if (methodPos > 0){
            String method = reqUrl.substring(methodPos + 12);
            mLogger.info("method:" + method);
            if (method.indexOf("/test/") >= 0){  // 如果是测试接口，一律放行
                return true;
            }
            else{
                String token = request.getHeader("Authorization");
                Cookie[] cookies = request.getCookies();
                if(cookies != null && cookies.length > 0){
                    for (Cookie cookie : cookies){
                        String ckName = cookie.getName();
                        if (ckName.equals("tk")){
                            token = cookie.getValue();
                            break;
                        }
                    }
                }
                if (!ObjectUtils.isEmpty(token)){
                    mLogger.info("Authorization:" + token);
                    String tkInfo = (String)mRedisTemplate.opsForValue().get(token);
                    System.out.println("tkInfo:" + tkInfo);   // token里
                    String[] arrInfo = tkInfo.split("\\|");
                    String userId = arrInfo[0];
                    String methodes = arrInfo[1];
                    mLogger.info("methodes:" + methodes);
                    if (methodes.indexOf(method) >= 0){   // 在权限对应的接口列表里存在，则通过
                        return true;
                    }
                }
            }
        }

        // 如果前面没有放行，则为非法访问
        PrintWriter out;
        JSONObject res = new JSONObject();
        res.put("state","false");
        res.put("msg","token is null or wrong");
        out = response.getWriter();
        out.append(res.toString());*/
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, @Nullable ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, @Nullable Exception ex) throws Exception {

    }
}
